package com.hbnu.mysql;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class DatabaseDemo6 {

    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;

        String driver;
        String url;
        String user;
        String password;

//        ResourceBundle resourceBundle = ResourceBundle.getBundle("jdbc");
//        driver = resourceBundle.getString("driverClass");
//        url = resourceBundle.getString("url");
//        user = resourceBundle.getString("user");
//        password = resourceBundle.getString("password");

        InputStream resourceAsStream = DatabaseDemo5.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        try {
            properties.load(resourceAsStream);
            driver = properties.getProperty("driverClass");
            url = properties.getProperty("url");
            user = properties.getProperty("user");
            password = properties.getProperty("password");

        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        try {
            Class.forName(driver);

            connection = DriverManager.getConnection(url,user,password);

            statement = connection.createStatement();

            //执行SQL语句
            String sql = "select * from nncc1";
            ResultSet resultSet = statement.executeQuery(sql);              //查询使用
            while (resultSet.next()) {
                String c1 = resultSet.getString(1);
                String c2 = resultSet.getString(2);
                String c3 = resultSet.getString(3);
                System.out.println(c1+"\t"+c2+"\t"+c3);

            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        finally {
            try {
                if (statement != null){
                    statement.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            try {
                if (connection != null){
                    connection.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
